/*
*       版权归 仓库所有者cc01cc(cc01cc@foxmail.com) 所有
*       此文件内的源码在遵循以下条例的前提下，提供自由使用：
*       一. 未经 仓库所有者cc01cc(cc01cc@foxmail.com) 书面许可:
*           1. 任何企业单位组织严禁将源码直接用于商业以及一切涉及获利的行为。
*       二. 未经 仓库所有者cc01cc(cc01cc@foxmail.com) 许可:
*           1. 任何企业单位组织严禁将源码以任何形式用于商业以及一些涉及获利的行为。
*           2. 任何个人严禁将源码以任何形式用于商业以及一些涉及获利的行为。
*       三. 申明:
*           1. 源码作者保留所有专利权，并开放专利使用权。
*           2. 若使用源码造成任何形式的损失，源码作者不承担任何责任。
*           3. 若由于使用源码造成任何形式的损失，由使用者提供全部赔偿。
*       四. 除上述之外，所有未定义事项遵循 AGPL-3.0 许可协议
*/

/*
 *     Copyright (c) 2021. cc01cc(cc01cc@foxmail.com). All rights reserved.
 *
 *     This is part of this file.
 *
 *     This file is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This file is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 0
#define F 1
#define n_round 3

#define Person_name char

// 使用单向循环链表
struct Circular_linked_list
{
    struct Circular_linked_list* next_node;
    Person_name name[23];
};

typedef struct Circular_linked_list* Linked_list;
typedef struct Circular_linked_list* Node;

int getData(Linked_list f_list, Linked_list m_list);
void printRound(Linked_list f_list, Linked_list m_list, int min_people);
int main()
{
    Linked_list f_list = (Linked_list)malloc(sizeof(struct Circular_linked_list));
    Linked_list m_list = (Linked_list)malloc(sizeof(struct Circular_linked_list));
    Node f_first = f_list;
    Node m_first = m_list;
    int min_people = 0;

    min_people = getData(f_list, m_list);
    if(min_people == 0)
    {
        printf("Can't match.\n");
        return 0;
    }
    // 将链表的指针，重新指向队头
    f_list = f_first;
    m_list = m_first;
    printRound(f_list, m_list, min_people);
    return 0;
}

void cutName(Person_name* name);
void initial(Linked_list list, Person_name* name);
Linked_list insert(Linked_list list, Person_name* name);
int determineGender(Person_name* name);
//***********************************************************
//  作用：返回人数较少一方的人员数量
//***********************************************************
int getData(Linked_list f_list, Linked_list m_list)
{
    int n_data;
    int i;
    int n_f = 0;
    int n_m = 0;
    Person_name person_name[23];
    freopen("dancer.txt", "r", stdin);
    // \n 需要读取换行符
    scanf("%d\n", &n_data);
    for(i = 0; i < n_data; i++)
    {
        gets(person_name);
        if(determineGender(person_name) == F)
        {
            n_f++;
            cutName(person_name);
            if(n_f==1)
            {
                initial(f_list, person_name);
            }else
            {
                f_list = insert(f_list, person_name);
            }
        }else
        {
            n_m++;
            cutName(person_name);
            if(n_m==1)
            {
                initial(m_list, person_name);
            }else
            {
                m_list = insert(m_list, person_name);
            }
        }
    }
    return (n_m > n_f)?(n_f):(n_m);
}

//***********************************************************
//  作用：输出每一轮的配对
//***********************************************************
void printName(Node f_node, Node m_node);
void printRound(Linked_list f_list, Linked_list m_list, int min_people)
{
    int i, j;
    Node f_node = f_list;
    Node m_node = m_list;
    for(i = 0; i < n_round; i++)
    {
        printf("\nRound %d: \n", i+1);
        for(j = 0; j < min_people; j++)
        {
            printf("%-20s%-20s\n",f_node->name, m_node->name);
            f_node = f_node->next_node;
            m_node = m_node->next_node;
        }
    }
}

//***********************************************************
//  作用：判断性别
//***********************************************************
int determineGender(Person_name* name)
{
    int name_lenth = strlen(name);
    if(name[name_lenth-2]=='F') return F;
    else return M;
}

//***********************************************************
//  作用：裁剪姓名
//***********************************************************
void cutName(Person_name* name)
{
    // 不同系统，存在差异，需要结合开发环境微调
    int name_lenth = strlen(name)-3;
    // 手动插入 \0 表示字符串结束
    name[name_lenth] = '\0';
}

// int isEmpty(Linked_list list)
// {
//     return list->name[0] == '0';
// }

void initial(Linked_list list, Person_name* name)
{
    list->next_node = list;
    strcpy(list->name, name);
}

Linked_list insert(Linked_list list, Person_name* name)
{
    Node node = (Node)malloc(sizeof(struct Circular_linked_list));
    strcpy(node->name, name);
    node->next_node = list->next_node;
    list->next_node = node;
    list = node;
    return list;
}
